package com.mdc.mansion.api.web.modules.order;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mdc.mansion.api.web.common.web.BaseController;
import com.mdc.mansion.common.response.Result;
import com.mdc.mansion.common.response.ResultCode;
import com.mdc.mansion.core.modules.basedata.RestaurantStaffReceiveDishesService;
import com.mdc.mansion.core.modules.dishes.RestaurantDishesFlavorService;
import com.mdc.mansion.core.modules.dishes.RestaurantDishesService;
import com.mdc.mansion.core.modules.dishes.RestaurantDishesStandardService;
import com.mdc.mansion.core.modules.order.KitchenDishesService;
import com.mdc.mansion.core.modules.order.dto.RestaurantStaffReceiveDishesDTO;
import com.mdc.mansion.core.modules.restaurant.RestaurantTableService;
import com.mdc.mansion.dataaccess.common.context.ContextHolder;
import com.mdc.mansion.dataaccess.modules.order.mo.RestaurantOderItemUnionMo;

/**<p>com.mdc.mansion.api.web.modules.dishes.KitchenDishesController</p>
 * <p>Title: KitchenDishesController</p>
 * <p>Copyright: Copyright © 2010-2014</p>
 * @version 1.0
 */
@Controller
@RequestMapping(value = "${adminPath}/kitchenDishes")
public class KitchenDishesController extends BaseController {
  
    @Resource
    private KitchenDishesService kitchenDishesService;
    @Resource
    private RestaurantDishesService restaurantDishesService;
    @Resource
    private RestaurantStaffReceiveDishesService restaurantStaffReceiveDishesService;
    @Resource
    private RestaurantDishesStandardService restaurantDishesStandardService;
    @Resource
    private RestaurantDishesFlavorService restaurantDishesFlavorService;
    @Resource
    private RestaurantTableService restaurantTableService;
    
    /**
     * 接口做调整，所有菜品均放到dishesList
     * comboList  不放数据
     * 数据结构与接口文档一致，增加了restaurant_dishes_combo_id 字段，如果非套餐则值为-1
     * 汇总条件: 订单,套餐,规格,口味
     * @param data
     * @return
     */
    @RequestMapping("/list")
    public Result list(String data){
        Result result = new Result(ResultCode.COMMON_SUCCESS);
        JSONObject obj = JSONObject.parseObject(data);
        Map<String,String> paraMap = Maps.newConcurrentMap();
        
        String status = obj.getString("status");
        
        String StaffId =  ContextHolder.currentRestaurantStaff().getId();
        if(StringUtils.isBlank(StaffId))
        {
        	result.setCode(ResultCode.COMMON_EXCEPTION);
        	return result;
        }
        String restaurantId = ContextHolder.currentRestaurant().getId();

//        String StaffId = "c35e808f68ef4717a25d0af65a236df2";
//        String restaurantId ="4caa57f359ce4f489fb47464e78af04b";
        
        paraMap.put("dishesStatus", status);
        paraMap.put("StaffId", StaffId);
        paraMap.put("restaurantId", restaurantId);
        
        List<RestaurantOderItemUnionMo> dishesList = kitchenDishesService.findKitchenDishesList(paraMap);
        result.setProperty("dishesList", dishesList);
        result.setProperty("comboList", "");
        return result;
    }
    
    @RequestMapping("/changeKitchenOfferFlag")
    public Result changeKitchenOfferFlag(String data){
        Result result = new Result(ResultCode.COMMON_SUCCESS);
        JSONObject obj = JSONObject.parseObject(data);
        String restaurantDishesId = obj.getString("restaurantDishesId");
       // int kitchenOfferFlag = obj.getIntValue("kitchenOfferFlag");
        String kitchenOfferFlag = obj.getString("kitchenOfferFlag");
        kitchenDishesService.changeKitchenOfferFlag(restaurantDishesId,kitchenOfferFlag);
        return result;
    }
    @RequestMapping("/changeStatus")
    public Result changeStatus(String data){
        Result result = new Result(ResultCode.COMMON_SUCCESS);
        JSONObject obj = JSONObject.parseObject(data);
        String[] orderItemArray = obj.getString("orderItemId").split(":");//订单ID
        String restaurantOrderId = obj.getString("restaurantOrderId");//订单ID
        String restaurantDishesId = obj.getString("restaurantDishesId");// 菜品ID
        String restaurantDishesComboId = obj.getString("restaurantDishesComboId");//套餐ID
        String dishesStatus = obj.getString("dishesStatus");//状态
        for(String orderItem:orderItemArray)
        {
        	kitchenDishesService.changeStatus(restaurantOrderId, restaurantDishesId, restaurantDishesComboId, dishesStatus,orderItem);
        }
        return result;
    }

    @RequestMapping("/changeReceiveDishes")
    public Result changeReceiveDishes (String data){
        Result result = new Result(ResultCode.COMMON_SUCCESS);
        JSONObject dataJsonObject = JSONObject.parseObject(data);
        JSONArray jsonArray = dataJsonObject.getJSONArray("dishes");
        List<String> restaurantDishesIds= Lists.newArrayList();
        if(jsonArray!=null && jsonArray.size()>1){
            for(int i=0;i<jsonArray.size();i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                restaurantDishesIds.add(jsonObject.getString("restaurantDishesId"));
            }
        }
        restaurantStaffReceiveDishesService.changeCurrentStaffReceiveDishes(restaurantDishesIds);
        return result;
    }

    @RequestMapping("/getReceiveDishes ")
    public Result getReceiveDishes (){
        Result result = new Result(ResultCode.COMMON_SUCCESS);
        List<RestaurantStaffReceiveDishesDTO>  restaurantStaffReceiveDishesDTOs= restaurantStaffReceiveDishesService.getCurrentStaffReceiveDishes();
        result.setProperty("receiveDishes",restaurantStaffReceiveDishesDTOs);
        return result;
    }
}
